import objc
from Foundation import *
from PyObjCTools import AppHelper

# Here the magic happens. This loads the Framework and creates a usable Python
# proxy object for us.
objc.loadBundle( "SkypeAPI", globals(), bundle_path=objc.pathForFramework( "~/Library/Frameworks/Skype.framework" ) )

# fetch the "simple delegate" protocol. See the SkypeAPI.h header which comes
# with the framework.
SkypeAPIDelegate = objc.protocolNamed( "SkypeAPIDelegate" )

# Create a informal protocol for the full delegate with callbacks. Without
# this class (?) as a mixin to the delegate class below we get nice segmentation
# faults when Skype tries to send us a message. The "signature" stuff is documented
# here: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html
SkypeAPIDelegateInformalProtocol = objc.informal_protocol(
    "SkypeAPIDelegateInformalProtocol",
    [
#- (void)skypeNotificationReceived:(NSString*)aNotificationString;
        objc.selector(
            None,
            selector='skypeNotificationReceived:',
            signature='v@:@',
            isRequired=0,
        ),
#- (void)skypeAttachResponse:(unsigned)aAttachResponseCode;             // 0 - failed, 1 - success
        objc.selector(
            None,
            selector='skypeAttachResponse:',
            signature='v@:I',
            isRequired=0,
        ),
#- (void)skypeBecameAvailable:(NSNotification*)aNotification;
        objc.selector(
            None,
            selector='skypeBecameAvailable:',
            signature='v@:@',
            isRequired=0,
        ),
#- (void)skypeBecameUnavailable:(NSNotification*)aNotification;
        objc.selector(
            None,
            selector='skypeBecameUnavailable:',
            signature='v@:@',
            isRequired=0,
        ),
    ]
)

class Delegate(NSObject, SkypeAPIDelegateInformalProtocol):
    """
    A delegate which gets the notifications from Skype.
    """
    def init(self, app_name):
        self = super( Delegate, self).init()
        if self is None:
            return None

        self._appname = app_name

        return self

    def clientApplicationName( self ):
        return self._appname

    def skypeAttachResponse_( self, code ):
        NSLog( "skypeAttachResponse: %d" % ( code ) )
        self.skype_attach_response( code )

    def skypeNotificationReceived_( self, nsstring ):
        NSLog( "skypeNotificationReceived: %s " % ( nsstring ) )
        self.skype_notification( nsstring )

    def skypeBecameAvailable_( self, notification ):
        NSLog( "skypeBecameAvailable: %s " % ( notification ) )
        self.skype_available( notification )

    def skypeBecameUnavailable_( self, notification ):
        NSLog( "skypeBecameUnavailable: %s" % ( notification ) )
        self.skype_unavailable( notification )

    # CBs to be overwritten
    def skype_attach_response(self, code ):
        pass

    def skype_notification( self, message ):
        pass

    def skype_available( self, message ):
        pass

    def skype_unavailable( self, message ):
        pass

# vim: set ts=4 sw=4 expandtab:
